package com.desire.homework;

import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author desire
 */
public class _27_查询ip所属地 {
    public static void main(String[] args) throws IOException {
        String ipAddress = getIpAddress("39.144.177.11");
        System.out.println(ipAddress);
    }

    public static String getIpAddress(String ipAddress) throws IOException {
        String url = "https://www.ip138.com/iplookup.asp?ip=" + ipAddress + "&action=2";
        String html = Jsoup.connect(url).get().toString();
        String address = "";
        Pattern pattern = Pattern.compile("\"ASN归属地\":\"[\u4e00-\u9fa5 ]+\"", 2);
        Matcher matcher = pattern.matcher(html);
        if (matcher.find()) {
            address = matcher.group().substring(10, matcher.group().length() - 1);
        }
        return address;
    }

}
